package com.doctcloud.hisinfo.common.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
//import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
/**
 * @ClassName MybatisPluspageConfig
 * @Author samula
 * @Descripe 分页插件配置
 * @Data 2024/6/27 0:17
 **/
@Configuration
@MapperScan("com.doctcloud.hisinfo.common.mapper")
public class MybatisPlusPageConfig {
//    @Bean
//    public MybatisPlusInterceptor mybatisPlusInterceptor(){
//        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        /**
         * 自定义字符集转换插件
         */
//        interceptor.addInnerInterceptor(new CharsetConversionInterceptor());
//        interceptor.addInterceptor(new CharsetConversionInterceptor());
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor() {
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.ORACLE));
            interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
            return interceptor;
        }

    /**
     * 配置 SqlSessionFactory，并将所有拦截器（包括MyBatis-Plus的和我们自定义的）都设置进去
     */
    @Bean
    public MybatisSqlSessionFactoryBean sqlSessionFactory(DataSource dataSource, MybatisPlusInterceptor mybatisPlusInterceptor) throws Exception {
        // 使用 MyBatis-Plus 提供的 SqlSessionFactoryBean
        MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);

        // 设置 MyBatis-Plus 的拦截器
        sessionFactory.setPlugins(mybatisPlusInterceptor);

        // 添加我们自定义的原生 MyBatis 插件
        // 注意：这里的 setPlugins 方法接收的是一个 Interceptor[] 数组
        sessionFactory.setPlugins(new Interceptor[]{new CharsetConversionInterceptor()});

        // 如果你有 mapper.xml 文件，请确保指定了正确的路径
        sessionFactory.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*.xml")
        );

        return sessionFactory;
    }
//        /**
//         * 分页插件
//         */
//        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.ORACLE));
//        /**
//         * 乐观锁插件
//         */
//        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
//        return interceptor;
//    }
}
